/*
 * Copyright (C) 2018 The Asiainfo-Ability Authors
 *
 *      Licensed under the Apache License, Version 2.0 (the "License");
 *      you may not use this file except in compliance with the License.
 *      You may obtain a copy of the License at
 *
 *          http://www.apache.org/licenses/LICENSE-2.0
 *
 *      Unless required by applicable law or agreed to in writing, software
 *      distributed under the License is distributed on an "AS IS" BASIS,
 *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *      See the License for the specific language governing permissions and
 *      limitations under the License.
 */

package org.asiainfo.ability.gateway.service.support;

import org.asiainfo.ability.gateway.cache.CacheService;
import org.asiainfo.ability.gateway.code.OnOffState;
import org.asiainfo.ability.gateway.dao.AbilityDao;
import org.asiainfo.ability.gateway.dao.ServiceDao;
import org.asiainfo.ability.gateway.dao.ClientDao;
import org.asiainfo.ability.gateway.model.Ability;
import org.asiainfo.ability.gateway.model.User;
import org.asiainfo.ability.gateway.model.UserAbility;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Author: visen
 * @Date: 2018/1/16
 * @Description:
 */
@Component
public class AbilityService {



    @Value("${ability.baseUrl}")
    private String baseUrl;

    public String getBaseUrl() {
        return baseUrl;
    }



    @Autowired
    private ClientDao clientDao;
    @Autowired
    private ServiceDao serviceDao;

    @Autowired
    private AbilityDao abilityDao;


    @Cacheable(CacheService.CACHE_USER)
    public User getUser(String clientId) {
        return clientDao.getUserByClientId(clientId);
    }


    @Cacheable(CacheService.CACHE_UAINFO)
    public UserAbility getUserAbility(String clientId, String ability) {
        List<UserAbility> userAbilities = serviceDao.listUserAbilities(clientId);
        for(UserAbility userAbility:userAbilities) {
            if(ability.equals(userAbility.getAbilityName())) {
                return userAbility;
            }
        }
        return null;
    }

    @Cacheable(CacheService.CACHE_ABILITYINFO)
    public Ability getAbilityByName(String name) {
        return abilityDao.loadAbilityByName(name);
    }


    @Cacheable(CacheService.CACHE_ABILITIES)
    public List<Ability> listAbilityRuntimeInfos() {
        return abilityDao.listAbilities();
    }



}
